/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.knowledge.plans;

import com.environment.Aim;
import com.environment.Percept;
import com.environment.Tile;
import com.knowledge.facts.Condition;
import com.knowledge.facts.Fact;
import com.knowledge.facts.FactRepository;
import com.knowledge.facts.FriendlyAntFact;
import com.knowledge.facts.UnexploredFact;
import com.knowledge.facts.paths.ExplorePathFact;
import com.utils.InvalidException;
import java.util.Collections;
import java.util.List;
import com.utils.WeightedDistance;
import java.util.ArrayList;
import java.util.Random;
import java.util.Set;

/**
 *
 * @author keithballantyne
 */
@SuppressWarnings("unchecked")
public class ExplorePlan extends Plan {
    Fact bestAnt = null;
    Fact bestTarget = null;

    public ExplorePlan() {
        // finding food requires we have an ant and an unexplored space.
        preconditions.add(new Condition(Condition.Op.ASSERT,FriendlyAntFact.class));
        
    }

    @Override
    public void calculateUtility() {
        List<Fact> unexplored = new ArrayList(FactRepository.getInstance().getInstances(UnexploredFact.class));
        Set<FriendlyAntFact> ants = FactRepository.getInstance().getFriendlyAnts();
        
        
        for (FriendlyAntFact ant: ants) {
            try {
                if (ant.getAllReferences().isEmpty()){
                    Random generator = new Random();
                    int deltaX = (generator.nextInt(3) - 1)*generator.nextInt(10);
                    int deltaY = (generator.nextInt(3) - 1)*generator.nextInt(10);  // up to 10 tiles away
                    Tile target = new Tile(ant.getLocation().getRow()+deltaY,ant.getLocation().getCol()+deltaX);
//                    System.err.println("delta x: "+deltaX+" delta y: "+deltaY);
                    List<Aim> directions = Percept.getInstance().getDirections(ant.getLocation(), target);

                    ExplorePathFact path = new ExplorePathFact(ant.getLocation(),target,directions);
                    path.setUtility(1);
                    FactRepository.getInstance().addInstance(path);
                    ant.setReference(path);
                }
                
//                movement.add(new Order(path.getSource(),path.getNextAim()));
            } catch (InvalidException ex) {
//                Logger.getLogger(GotoFoodPlan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
         
         
   
    }
    
}
